import pandas as pd
import requests

# 读取CSV文件
df = pd.read_csv('pois_data.csv')

# 定义起点坐标和API密钥
origin = "114.273599,30.596733"
key = "75c9bb0e23bb178aaf5926e95e2dc8a4"
url_template = "https://restapi.amap.com/v5/direction/driving?origin={}&destination={}&key={}&destination_id=070500"

# 用于存储结果的列表
results = []

# 遍历DataFrame中的每一行
for index, row in df.iterrows():
    # 构造请求的URL
    destination = f"{row['longitude']},{row['latitude']}"
    url = url_template.format(origin, destination, key)

    # 发送请求
    response = requests.get(url)

    # 解析响应内容
    res_data = response.json()

    # 检查API返回的状态码
    if res_data.get('status') == "1":  # 假设成功的状态码是"1"
        # 获取count和distance信息
        count = res_data['count']
        distance = res_data['route']['paths'][0]['distance']
        # 将结果添加到列表中
        results.append({
            'name': row['name'],
            'longitude': row['longitude'],
            'latitude': row['latitude'],
            'count': count,
            'distance': distance
        })
    else:
        print(f"Error for index {index}: {res_data.get('info')}")

# 使用结果更新DataFrame
df_results = pd.DataFrame(results)

# 将新的列添加到原始DataFrame中
df_updated = pd.concat([df, df_results[['count', 'distance']]], axis=1)

# 保存更新后的DataFrame到新的CSV文件
df_updated.to_csv('武银大厦2.csv', index=False)